﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS
{
    public partial class Ins_EditContact : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[3].Selected = true;
            if (!IsPostBack)
            {
                id = (string)Session["userName"];

                if (id == null)
                    Response.Redirect("../login.aspx");

                // Connect to the Database
                conn = new MySqlConnection(strProvider);
                conn.Open();

                string strSQL = "SELECT S.fs_name, S.fs_pass, S.fs_email, S.fs_url FROM Staff S WHERE S.fsid = " + id;

                sqlCommand = new MySqlCommand(strSQL, conn);
                readVals = sqlCommand.ExecuteReader();

                while (readVals.Read())
                {
                    NameBox.Text = readVals.GetString("fs_name");
                    PassBox.Text = readVals.GetString("fs_pass");
                    EmailBox.Text = readVals.GetString("fs_email");
                    URLBox.Text = readVals.GetString("fs_url");
                }
                readVals.Close();
                conn.Close();
            }
            
        }

        protected void apply_click(object sender, EventArgs e)
        {
            id = (string)Session["userName"];

            if (id == null)
                Response.Redirect("../login.aspx");

            // Connect to the Database
            MySqlConnection conn2 = new MySqlConnection(strProvider);
            conn2.Open();

            string strSQL = "UPDATE Staff "
                                + "SET fs_name='" + NameBox.Text + "', fs_email='" + EmailBox.Text + "', fs_pass='" + PassBox.Text + "', fs_url='" + URLBox.Text + "' "
                                + "WHERE fsid=" + id;

            MySqlCommand sqlCommand2 = new MySqlCommand(strSQL, conn2);
            try
            {
                sqlCommand2.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
                string msg = "Update Error: ";
                msg += ex.Message + "<br>";
                ChangedLabel.Text = msg;
                ChangedLabel.Visible = true;
                goto skip;
            }
            ChangedLabel.Text = "Values Changed!<br>";

            skip:

            strSQL = "SELECT S.fs_name, S.fs_pass, S.fs_email, S.fs_url FROM Staff S WHERE S.fsid = " + id;

            sqlCommand = new MySqlCommand(strSQL, conn2);
            readVals = sqlCommand.ExecuteReader();

            while (readVals.Read())
            {
                NameBox.Text = readVals.GetString("fs_name");
                PassBox.Text = readVals.GetString("fs_pass");
                EmailBox.Text = readVals.GetString("fs_email");
                URLBox.Text = readVals.GetString("fs_url");
            }
            readVals.Close();

            conn2.Close();
        }
    }
}